var WxParse = require('../../../../wxParse/wxParse.js');

var app = getApp()
Page({
  data: {
    info: {}
  },
  onLoad: function (options) {
    if (options && options.id) {
      this.loadDetail(options.id)
    }
  },
  openWechatMap(e) {
    let self = this
    wx.getSetting({
      success: (res) => {
        var auth = res.authSetting['scope.userLocation']
        console.log(auth)
        if (auth != undefined && auth != true) {
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
            success: function (resc) {
              if (resc.confirm) {
                wx.openSetting({
                  success: function (data) {
                    console.log(data)
                    if (data.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 5000
                      })
                      //再次授权，调用getLocationt的API
                      self.showMap(self)
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'success',
                        duration: 5000
                      })
                    }
                  }
                })
              } else {
                console.log("拒绝授权")
              }
            }
          })
        } else {
          self.showMap(self)
        }
      }
    })
  },
  showMap(self) {
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        wx.openLocation({
          latitude: Number(self.data.info.map_x),
          longitude: Number(self.data.info.map_y),
          name: self.data.info.name,
          address: self.data.info.address_full
        })
      }
    })
  },
  onShow: function (options) {
  },
  loadDetail(id) {
    let self = this
    app.request({
      url: '/home/api/hotelAroundDetail',
      method: 'POST',
      data: {
        around_id: id
      },
      success: (res) => {
        var article = res.result.detail;
        if (article != null){
          WxParse.wxParse('article', 'html', article, self, 5);
        }
        self.setData({
          info: res.result
        })
        
        //console.log(this.data.info)
        wx.setNavigationBarTitle({
          title: this.data.info.name,
        })
      }
    })
  },
  callPhone(e) {
    var phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
      success: () => {
      }
    })
  }
})